//
//  ImageLayerCell.m
//  AsyncImages
//
//  Created by alex tao on 2/16/12.
//  Copyright (c) 2012 develop4u.gr. All rights reserved.
//

#import "ImageLayerCell.h"

#import "SHKItem.h"
#import "SHKActionSheet.h"

#define TOOLBAR_HEIGHT      25
#define SHARE_WIDTH         70
#define CAI_WIDTH           60
#define DING_WIDTH          60


@interface myButton : UIButton {
@private
    
}
@end

@implementation myButton

- (CGRect)imageRectForContentRect:(CGRect)contentRect {
    return CGRectMake(0, 5, 20, 15);
}

- (CGRect)titleRectForContentRect:(CGRect)contentRect {
    return CGRectMake(25, 0, contentRect.size.width-25, TOOLBAR_HEIGHT);
}

@end

///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

@implementation ImageLayerCell

- (void)btnClick:(id)sender;
{
    UIButton * btn = (UIButton*)sender;
    switch (btn.tag) {
        case 1:
        {
            SHKItem *item = [SHKItem image:_imageView.imgView.image title:@"share test"];
            
            SHKActionSheet *actionSheet = [SHKActionSheet actionSheetForItem:item];
            actionSheet.destructiveButtonIndex = 0;
            [actionSheet showInView:self.window];
        }
        break;
            
        case 2:
            NSLog(@"Cai Cai Cai Cai ");
            break;
            
        case 3:
            NSLog(@"Ding Ding Ding Ding ");
            break;
            
        default:
            break;
    }
}

- (UIButton*) buttonWithImage:(NSString*)imgName1 image2:(NSString*)imgName2 andTitle:(NSString*)title
{
    myButton * btn = [myButton buttonWithType:UIButtonTypeCustom];
    btn.frame = CGRectMake(0, 0, 50, TOOLBAR_HEIGHT);
    btn.titleLabel.font = [UIFont systemFontOfSize:13];
    
    [btn setImage:[UIImage imageNamed:imgName1] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:imgName2] forState:UIControlStateSelected];
    [btn setTitle:title forState:UIControlStateNormal];
    
    btn.backgroundColor = [UIColor grayColor];

    [btn addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];

    return btn;
}

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
        
        NSUInteger originY = self.bounds.size.height - TOOLBAR_HEIGHT;
        NSUInteger originX = self.bounds.size.width - SHARE_WIDTH;
        
        _imageView = [[UIImageLayer alloc] initWithFrame:CGRectMake(0, 0, self.bounds.size.width, originY)];
        [self addSubview:_imageView];

        _btnShare = [self buttonWithImage:@"share_btn_normal_iphone.png" image2:@"share_btn_down_iphone.png" andTitle:@"转发"];
        _btnShare.frame = CGRectMake(originX, originY, SHARE_WIDTH, TOOLBAR_HEIGHT);
        _btnShare.tag = 1;
        [self addSubview:_btnShare];
        
        originX -= CAI_WIDTH;
        _btnCai = [self buttonWithImage:@"un_rate_btn.png" image2:@"un_rate_btn.png" andTitle:@"踩"];
        _btnCai.frame = CGRectMake(originX, originY, CAI_WIDTH, TOOLBAR_HEIGHT);
        _btnCai.tag = 2;
        [self addSubview:_btnCai];
        
        originX -= DING_WIDTH;
        _btnDing = [self buttonWithImage:@"rate_btn.png" image2:@"rate_btn.png" andTitle:@"顶"];
        _btnDing.frame = CGRectMake(originX, originY, DING_WIDTH, TOOLBAR_HEIGHT);
        _btnDing.tag = 3;
        [self addSubview:_btnDing];
        
    }
    return self;
}

- (void)dealloc
{
    [_imageView release];
    
    [super dealloc];
}

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect
{
    // Drawing code
}
*/

- (void) setImage:(NSString*)imgUrl
{                    
    [_imageView initImage:imgUrl useOriginalSize:FALSE ratioMode:TRUE layerInfo:Nil];
}

- (void) setCountDing:(NSUInteger)dingCnt cai:(NSUInteger)caiCnt zhuan:(NSUInteger)zhuanCnt
{
    [_btnDing setTitle:[NSString stringWithFormat:@"顶%d", dingCnt] forState:UIControlStateNormal];
    [_btnCai setTitle:[NSString stringWithFormat:@"踩%d", caiCnt] forState:UIControlStateNormal];
    [_btnShare setTitle:[NSString stringWithFormat:@"转发%d", zhuanCnt] forState:UIControlStateNormal];
}

@end
